package com.xxxy.jsq.action.role;

import com.xxxy.jsq.dao.RoleDao;
import com.xxxy.jsq.model.Role;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/deleteRoleServlet")
public class deleteRoleServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public deleteRoleServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置字符编码，避免中文乱码
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        // 创建角色相关对象
        Role model = new Role();
        RoleDao dao = new RoleDao();

        // 获取页面传递的角色ID参数（与前端删除按钮传递的参数名保持一致）
        Long cid = Long.valueOf(request.getParameter("cid"));

        // 调用DAO层的删除方法（根据ID删除角色）
        String flag = dao.deleteById(cid);

        // 将删除结果返回给前端
        PrintWriter out = response.getWriter();
        out.print(flag);
        out.flush();
        out.close();
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // POST请求也交给doGet处理，保持与部门删除的处理方式一致
        doGet(request, response);
    }
}